package org.zk.dubbo_provider.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.zk.dubbo_provider.dao.CourseMapper;
import org.zk.entity.Course;
import org.zk.entity.Lession;
import org.zk.service.CourseService;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@Service(timeout = 10000)
@CacheConfig(cacheNames = "course")
public class CourseServiceImpl implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    //课程发布
    @Override
    public int insertCourse(Course course) {
        return courseMapper.addCourse(course);
    }

    //课程详情页
    @Override
    public Course queryCourse(int courseId) {
        return courseMapper.queryCourse(courseId);
    }

    //更新课程评价数
    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateCourse(course);
    }

    //查询单个教师发布的课程数量
    @Override
    public int getCourseCountByTid(Map map) {
        return courseMapper.getCourseCountByTid(map);
    }

    //查询单个教师发布的所有课程
    @Override
    public PageInfo getqueryCourseByTidPage(Map<String, Object> map) {
        PageInfo page=new PageInfo();
        if (map.get("pageSize") != null){
            int count=courseMapper.getCourseCountByTid(map);
            page.setTotal(count);
            int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
            page.setPageSize(pageSize);
            int currPage = Integer.parseInt(String.valueOf(map.get("pageNum")));
            page.setPageNum(currPage);

            //计算总页数
            int a ;
            if (count % pageSize > 0) {
                a = count / pageSize + 1;
            } else {
                a = count / pageSize;
            }
            page.setPages(a);

            //开始分页
            PageHelper.startPage(currPage,pageSize);
            List<Course> ls= courseMapper.queryCourseByTid(map);
            page.setList(ls);
        }else {
            List<Course> ls= courseMapper.queryCourseByTid(map);
            page.setList(ls);
        }
        return page;
    }

    //查询单个教师发布的最新的一个courseId
    @Override
    public Course queryCourseIdByTid(int teacherId) {
        return courseMapper.queryCourseIdByTid(teacherId);
    }


    //分页查询所有课程
    @Override
    public PageInfo getAllCoursePage(Map<String, Object> map) {
        PageInfo page=new PageInfo();
        int count=courseMapper.queryCourseCount(map);
        page.setTotal(count);
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);
        int currPage = Integer.parseInt(String.valueOf(map.get("pageNum")));
        page.setPageNum(currPage);

        //计算总页数
        int a ;
        if (count % pageSize > 0) {
            a = count / pageSize + 1;
        } else {
            a = count / pageSize;
        }
        page.setPages(a);

        //开始分页
        PageHelper.startPage(currPage,pageSize);
        List<Course> ls= courseMapper.getAllList(map);
        page.setList(ls);
        return page;
    }

    //course表审核
    @Override
    public int updateFlag(Course course) {
        return courseMapper.updateFlag(course);
    }

    @Override
    public PageInfo getAllCoursesPage(Map map) {
        PageInfo<Course> pageInfo;
        int pageSize=Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum=Integer.parseInt(String.valueOf(map.get("pageNum")));
        PageHelper.startPage(pageNum,pageSize);
        try {
            List<Course> list= courseMapper.getAllCoursePages(map);
            pageInfo = new PageInfo<Course>(list,pageSize);

        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        System.out.println("totaltotal===="+pageInfo.getTotal());

        return pageInfo;
    }



    //分条件查询单个教师发布的课程
    @Override
    public List<Course> getCourseByTidAndFlag(Map<String, Object> map) {
        return courseMapper.getCourseByTidAndFlag(map);
    }

    @Override
    public int delCourseById(int courseId) {
        return courseMapper.delCourseById(courseId);
    }

    @Override
    public List<Course> getCourseByTypeId(Map map) {
        System.out.println(map.get("typeId"));
        return courseMapper.getCourseByTypeId(map);
    }



    @Override
    public List<Course> getCourseBySid(int id) {
        return courseMapper.getCourseBySid(id);
    }
    //得到当前courseId


   /* //高申 2019-12-16 查看所有
    @Override
    public List<Course> queryAllCourse() {
        return courseMapper.queryAllCourse();
    }*/

    //高申 2019-12-16 根据id查看课程
    @Override
    public Course queryOneCourse(int courseId) {
        return courseMapper.queryOneCourse(courseId);
    }
    //高申 2019-12-17 支付成功，课程销量+1
    @Override
    public void updateCourseCount(Course course) {
        courseMapper.updateCourseCount(course);
    }


    /**
     * @MethodName: getAllCourseList
     * @Description: 综合查询所有视频课程
     * @Param: [map]
     * @Return: com.github.pagehelper.Page
     * @Author: 刘艺广
     * @Time: 2020/1/7 0007 下午 5:28
     **/
    @Override
    public List<Course> getAllCourseList(Map map) {
        List<Course> courseList= courseMapper.getAllCourseList(map);
        return courseList;
    }

    public CourseMapper getCourseMapper() {
        return courseMapper;
    }

    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }
    
}
